package com.seth.myapplication.login;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.seth.myapplication.LoginActivity;
import com.seth.myapplication.MainActivity;
import com.seth.myapplication.R;
import com.seth.myapplication.bean.User;
import com.seth.myapplication.service.NetWorkService;

/**
 * Created by Seth on 2017/9/26.
 */

public class LoginPage extends Fragment {

    private Button loginButton;
    private EditText loginAccount;
    private EditText loginPassword;
    private TextView registTxt;
    private TextView findPassTxt;
    private User mUser;

    private FragmentTransaction transaction;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_login2, container, false);
        initView(view);
        initEvent();

        return view;
    }


    private void initView(View view) {
        loginAccount = (EditText) view.findViewById(R.id.login_account);
        loginPassword = (EditText) view.findViewById(R.id.login_password);
        registTxt = (TextView) view.findViewById(R.id.regist_txt);
        findPassTxt = (TextView) view.findViewById(R.id.find_password_txt);
        loginButton = (Button) view.findViewById(R.id.login_btn);

        Drawable drawable1 = getResources().getDrawable(R.drawable.ic_account);
        Drawable drawable2 = getResources().getDrawable(R.drawable.ic_password);

        drawable1.setBounds(0,0,60,60);
        loginAccount.setCompoundDrawables(drawable1,null,null,null);
        drawable2.setBounds(0,0,60,60);
        loginPassword.setCompoundDrawables(drawable2,null,null,null);

        transaction = getFragmentManager().beginTransaction();

    }

    private void initEvent() {

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mUser = new User();
                mUser.setPhoneNum(loginAccount.getText().toString());
                mUser.setPassword(loginPassword.getText().toString());
                new NetWorkService(getActivity(),LoginActivity.LOGIN_REGISTER).usrLogin(mUser);
            }
        });

        registTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment fragment = new RegistPage();
                //Fragment fragment1 = Login.getContentFragment();

                setFragment(fragment);
            }
        });

        findPassTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment fragment = new FindPassPage();

                setFragment(fragment);
            }
        });


    }


    public void setFragment(Fragment fragment) {

        transaction.replace(R.id.activity_login,fragment);
        //transaction.hide(fragment1).add(R.id.activity_login, fragment);
        LoginActivity.setContentFragment(fragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }
}
